package com.timothy.junk.cbond.activity;

/**
 * Created by qtang on 7/2/13.
 */
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.markupartist.android.widget.PullToRefreshListView;
import com.sina.oefund.SinaOEFundClient;
import com.sina.stock.SinaStockClient;
import com.timothy.junk.R;
import com.timothy.gfund.data.CBondCalculator;
import com.timothy.junk.ActivityManage;
import com.timothy.junk.adapter.CBondAdaptor;
import com.timothy.junk.cbond.bean.CBond;
import com.timothy.junk.cbond.bean.CBondResult;
import com.timothy.junk.common.parse.CBondParser;
import com.timothy.junk.common.util.NetWorkUtil;

public class CBondList extends ListActivity {
	public static final String LOG_TAG = "CBondList";

    Context mContext;
    CBondAdaptor adaptor ;
    
    ProgressBar progressBar;
    
    List<CBond> cBondList;
    List<CBondResult> cBondResultList;
    
    SinaStockClient mClient;
    SinaOEFundClient fClient;
    

    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cbond_list);
        
        mContext = getApplicationContext();
        ActivityManage.add(this);
        
        progressBar =(ProgressBar) findViewById(R.id.loading_spinner);
        
        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new GetDataTask().execute();
            }
        });

        if(NetWorkUtil.isNetworkAvailable(mContext)){
        	new GetDataTask().execute();
        }else{
        	Toast.makeText(mContext, "Your network is not available!", Toast.LENGTH_LONG).show();
        }
        
        ((PullToRefreshListView) getListView()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(LOG_TAG,"-----onItemClick()-----");
//				Intent intent=new Intent(mContext, GFundDetail.class);
//				GFundBean sBean = gFundList.get(arg2-1);
//				intent.putExtra("index",sBean.getIndex());
//				startActivity(intent);
			}
        });
    }
 
    
//    public void loadData(){
//    	cBondList = CBondParser.getCBondList(mContext);
//    	CBondCalculator cal = new CBondCalculator(cBondList);
//        cal.initial();
//        cal.getStockData(SinaStockClient.getInstance());
//        cal.calculate();
//        cBondResultList = cal.getcBondResultList();
//    }
    
    CBondCalculator cal;
    
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
    	
        @Override
		protected void onPreExecute() {
			super.onPreExecute();
			cBondList = CBondParser.getCBondList(mContext);
			cal = new CBondCalculator(cBondList);
	        cal.initial();
		}
    	
        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
//                loadData();
                cal.getStockData(SinaStockClient.getInstance());
            } catch (InterruptedException e) {
            }
            return null;
        }

		@Override
        protected void onPostExecute(String[] result) {
            super.onPostExecute(result);
            progressBar.setVisibility(View.GONE);
            
            //calculate by data
            cal.calculate();
            cBondResultList = cal.getcBondResultList();
            
            if(cBondResultList!=null&&cBondResultList.size()>0){
            	if(adaptor == null){
                    adaptor = new CBondAdaptor(mContext, cBondResultList);
                    setListAdapter(adaptor);           		
            	}else{
            		adaptor.notifyDataSetChanged();
            	}
                ((PullToRefreshListView) getListView()).onRefreshComplete();
            }else{
            	Toast.makeText(mContext, "Can not get any data!", Toast.LENGTH_LONG).show();
            }
        }
    }

}
